import Mock from 'mockjs';

//表单初始数据
function randomOrderState(){
    return Mock.mock({ "state|1":[
        "已发货",
        "未发货",
        "已签收"
    ]})
}
function randomDesc(){
    const phoneArr = ['vivo','apple','HUAHEI','oppo','honor','Redmi','mi','realme','iqoo','ONEPLUS','SAMSUNG','meizu'];
    const phoneNum = Math.ceil(Math.random()*5);
    let orderDesc = [];
    for(let i=0;i<phoneNum;i++){
        let arrIndex = Math.floor(Math.random()*12);
        if(-orderDesc.indexOf(phoneArr[i])){
            orderDesc.push(`${phoneArr[arrIndex]}×${Math.ceil(Math.random()*5+1)}, `)
        }
    }
    orderDesc = orderDesc.join('');
    return orderDesc
}
let List = [];
let searchList = null;
let stateList = null;
const count = 200;
for (let i=0;i<count;i++){
    List.push(
        Mock.mock({
            id:Mock.Random.guid(),
            orderNum:Mock.Random.float(1000000,9999999,0,0),
            date:Mock.mock('@date("yyyy-MM-dd")'),
            orderName:Mock.mock('@cname()'),
            orderPho:`${Mock.Random.float(100,999,0,0)}-${Mock.Random.float(1000,9999,0,0)}-${Mock.Random.float(1000,9999,0,0)}`,
            orderState:randomOrderState().state,
            desc:randomDesc(),
        })
    )
}
List.sort((a,b)=>{
    return new Date(b.date)-new Date(a.date)
})
let isDone = 1;

export default{
        
    //刷新获取数据
    getOrderData:() => {
        return List
    },

    //新增订单
    addOrderData:config => {
        const {orderNum,date,orderName,orderPho,orderState,desc} = JSON.parse(config.body);
        List.unshift({
            id:Mock.Random.guid(),
            orderNum,
            date:date.slice(0,10),
            orderName,
            orderPho,
            orderState,
            desc
        })
        List.sort((a,b)=>{
            return new Date(b.date)-new Date(a.date)
        })
        return List;
    },

    //删除订单
    deleteOrderData:config => {
        const {id} = JSON.parse(config.body);
        List = List.filter(item => item.id !== id);
        if(searchList){
            searchList = searchList.filter(item => item.id !== id);
            return searchList;
        }else{
            return List;
        }
    },

    //编辑订单
    editOrderData:config => {
        const {id,orderNum,date,orderName,orderPho,orderState,desc} = JSON.parse(config.body);
        List.forEach(item => {
            if(item.id === id){
                item.orderNum = orderNum;
                item.date = date;
                item.orderName = orderName;
                item.orderPho = orderPho;
                item.orderState = orderState;
                item.desc = desc;
            }
        })
        List.sort((a,b)=>{
            return new Date(b.date)-new Date(a.date)
        })
        if(searchList){
            searchList.forEach(item => {
                if(item.id === id){
                    item.orderNum = orderNum;
                    item.date = date;
                    item.orderName = orderName;
                    item.orderPho = orderPho;
                    item.orderState = orderState;
                    item.desc = desc;
                }
            })
            searchList.sort((a,b)=>{
                return new Date(b.date)-new Date(a.date)
            })
            return searchList
        }else{
            return List;
        }
    },

    //搜索订单
    searchOrderData:config => {
        const {searchStr} = JSON.parse(config.body);
        if(searchStr){
            searchList = List.filter((item)=>{
                return item.orderName.indexOf(searchStr) != -1;
            })
            return searchList
        }else{
            searchList = null;
            return List;
        }
    },

    //日期排序
    changeSort:config => {
        const {order} = JSON.parse(config.body);
        if(order === 'descending' && isDone == -1){
            isDone = 1;
            List.reverse();
            if(searchList){
                searchList.reverse();
                return searchList;
            }
            return List;
        }else if(order === 'ascending' && isDone == 1){
            isDone = -1;
            List.reverse();
            if(searchList){
                searchList.reverse();
                return searchList;
            }
            return List;
        }else{
            if(searchList){
                return searchList
            }
            return List
        }
    },

}

